package com.slashvision.im

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.activityViewModels
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.google.accompanist.insets.ui.Scaffold
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.slashvision.im.chat.*
import com.slashvision.im.contact.ContactViewModel
import com.slashvision.im.contact.PageAllContacts
import com.slashvision.im.contact.PageAllConv
import com.slashvision.im.profile.PageProfile
import com.slashvision.im.ui.UText

class IMMainActivity : InitIMActivity() {
    private val chatVM by viewModels<ChatViewModel>()
    private val contactVM by viewModels<ContactViewModel>()

    @ExperimentalPermissionsApi
    @ExperimentalPagerApi
    @ExperimentalComposeUiApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            NavHost(navController = navController, startDestination = ROUTE_PAGE_MAIN) {
                composable(ROUTE_PAGE_MAIN) { IMMain(contactVM = contactVM) }
                composable(ROUTE_CHAT_UI) { PageChat(vm = chatVM, navController = navController) }
                composable("$ROUTE_IMAGE_PREVIEW?isNet={isNet}&path={path}", arguments = listOf(
                    navArgument("isNet") {
                        type = NavType.BoolType
                        defaultValue = true
                    },
                    navArgument("path") { type = NavType.StringType }
                )) {
                    val isNet = it.arguments?.getBoolean("isNet", true)
                    val path = it.arguments?.getString("path")
                    path?.let {
                        PageImagePreview(
                            isNetImage = isNet?:true,
                            path = path,
                            navController = navController
                        )
                    }
                }
            }
        }
    }
}

const val ROUTE_PAGE_MAIN = "_ROUTE_PAGE_MAIN_"

@ExperimentalComposeUiApi
@ExperimentalPagerApi
@Composable
fun IMMain(
    contactVM: ContactViewModel,
) {
    var bottomNavSelected by remember { mutableStateOf<Int>(0) }
    val pagerState = rememberPagerState()
    Scaffold(bottomBar = {
        BottomNavigation {
            BottomNavigationItem(
                selected = bottomNavSelected == 0,
                onClick = { bottomNavSelected = 0 },
                icon = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_message),
                        contentDescription = null
                    )
                },
                label = { UText(text = "会话") })
            BottomNavigationItem(
                selected = bottomNavSelected == 1,
                onClick = { bottomNavSelected = 1 },
                icon = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_contacts),
                        contentDescription = null
                    )
                },
                label = { UText(text = "通讯录") })
            BottomNavigationItem(
                selected = bottomNavSelected == 2,
                onClick = { bottomNavSelected = 2 },
                icon = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_profile),
                        contentDescription = null
                    )
                },
                label = { UText(text = "我的") })

        }
    }) {
        HorizontalPager(count = 3, state = pagerState) {
            when (pagerState.currentPage) {
                0 -> {
                    PageAllConv(vm = contactVM) {

                    }
                }
                1 -> {
                    PageAllContacts(vm = contactVM) {

                    }
                }
                2 -> {
                    PageProfile()
                }
            }
        }
    }
}